package com.ricohgag.controller.common;

import com.ricohgag.util.Msg;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ricohgag
 * @date 2018/8/30 10:35
 */
@RestController
@RequestMapping("/common/uploadFile")
public class UploadFile {

    @RequestMapping("/index")
    public Msg uploadFile(HttpServletRequest request, HttpServletResponse response){
        //这是一根平台的斜杠
        String separator = File.separator;

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：
        MultipartFile mnFile = multipartRequest.getFile("uploadFile");
        String loadName = mnFile.getOriginalFilename();
        String ext=loadName.substring(loadName.lastIndexOf("."),loadName.length());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmSSS");
        String fileName=simpleDateFormat.format(new Date()).concat(new Random(5000).nextInt()+"").concat(ext);
        String rootContext=request.getSession().getServletContext().getRealPath("/");
        String uploadsName="upload";
        simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String childerDateDir=simpleDateFormat.format(new Date());
        if (!new File(rootContext.concat(separator).concat(uploadsName)).exists()) {
            new File(rootContext.concat(separator).concat(uploadsName)).mkdirs();
        }

        if (!new File(rootContext.concat(separator).concat(uploadsName).concat(separator).concat(childerDateDir)).exists()) {
            new File(rootContext.concat(separator).concat(uploadsName).concat(separator).concat(childerDateDir)).mkdirs();
        }

        String path=rootContext.concat(separator).concat(uploadsName).concat(separator).concat(childerDateDir);
        File file=new File(path,fileName);
        try {
            mnFile.transferTo(file);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        String savePath="".concat(separator).concat(uploadsName).concat(separator).concat(childerDateDir).concat(separator).concat(fileName);
        Map<String,Object> result=new HashMap<String, Object>();
        result.put("file_ext",ext);
        result.put("file_size", mnFile.getSize());
        result.put("original_filename", loadName);
        result.put("_filename", fileName);
        result.put("savepath", savePath);
        result.put("url",savePath);
        result.put("upload_rootpath","/"+uploadsName+"/");
        result.put("server_display_url", "");
        result.put("group_name",groupName(ext));
        Msg msg=new Msg();
        msg.setData(result);
        msg.setStatus(1);
        msg.setMsg("上传成功！");

        return msg;
    }

    /**
     * 识别是否为图片格式
     * @param ext
     * @return
     */
    private String groupName(String ext){
        String [] grouByName=".gif,.jpg,.jpeg,.bmp,.png".split(",");
        List<String> data=Arrays.asList(grouByName);
        boolean flag=data.contains(ext);
        if(flag){
            return "images";
        }else {
            return "file";
        }
    }

}
